/**
 * 公共的js文件
 * @author zlc
 */
window.onload=function() {
    layui.define(['element', 'form','laydate','table','upload','layedit'], function (exports) {
        var table = layui.table;
        var form = layui.form;
        var layer = layui.layer;
        var laydate = layui.laydate;

        /**
         * 手动排序
         */
        $(document).on('blur', '.wy-input-blur', function () {
            var that = $(this);
            var _data = that.attr('wy-data');
            var data_json = eval('(' + _data + ')');
            var n_value = that.val();
            var o_value = data_json.def_value || '';
            if (n_value == o_value) {
                return;
            }
            if (data_json.zz) {
                var zz = new RegExp(data_json.zz);
                var zz_title = data_json.zz_title || "请输入有效信息";
                if (!zz.test(n_value)) {
                    layer.msg(zz_title);
                    return;
                }
            }
            $.post(data_json.url, {"val": n_value}, function (data) {
                console.log(data);
                if (data.code == 0) {//下次统一成code msg
                    layer.msg(data.msg);
                    table.reload('dataTable', {
                        where: {}, page: {curr: 1}
                    }); //只重载数据
                }else{
                    layer.msg(data.msg);
                    return;
                }
            }, "JSON");
        });

        /**
         * 修改状态
         */
        form.on('checkbox(lay-but-checkbox)', function (data) {
            var that = $(this);
            var _data = that.attr('wy-data');
            var data_json = eval('(' + _data + ')');
            var n_value = that.val();
            $.post(data_json.url, {"id": n_value}, function (data) {
                if (data.code == 0) {
                    layer.msg(data.msg, {
                        time: 1000,
                        end: function () {
                            table.reload('dataTable',{ where: {}}); //只重载数据
                        }
                    });
                    return;
                } else {
                    layer.msg(data.msg);
                    return;
                }
            }, "json");
            form.render('checkbox');
        });

        /**
         * 图片预览
         */
        $(document).on('click', '.layui-img-click', function () {
            var img = $(this).attr("src");
            var title = $(this).attr("alt");
            if (!img) {
                return;
            }
            layer.photos({
                shade: 0.3,
                photos: {
                    "title": title,
                    "start": 0,
                    "data": [
                        {
                            "src": img,
                        }
                    ]
                }
                , anim: 5
            });
        });

        /**
         * 单个删除数据
         */
        $(document).on('click', '.layui-data-delete', function () {
            var that = $(this);
            var _data = that.attr('wy-data');
            var data_json = eval('(' + _data + ')');
            layer.confirm('删除后，将永久无法恢复，确定删除吗?', function (index) {
                $.post(data_json.url, {_method: 'delete', id: data_json.id}, function (result) {
                    if (result.code == 0) {
                        table.reload('dataTable'); //只重载数据
                    }
                    layer.close(index);
                    layer.msg(result.msg);
                },"json");
            });
        });


        /**
         * 时间设置
         */
        //时间范围段
        $('.enet_daterange').each(function () {
            laydate.render({
                elem: this,
                type: 'datetime'
                , range: true
            });
        });
        //设置开始时间段
        $('.enet_daterange_limit').each(function () {
            var min_time = $(this).attr('lay-mintime');
            laydate.render({
                elem: this,
                type: 'datetime'
                , range: true,
                min:min_time
            });
        });

        // 年月日 时分秒
        $('.enet_datetime').each(function(){
            laydate.render({
                elem: this,
                type: 'datetime'
            });
        });

        // 年月日
        $('.enet_date').each(function(){
            laydate.render({
                elem: this,
                type: 'date'
            });
        });

        /*富文本编辑器*/
        var LayEditInst = [];
        function layedit_init(div_id){
                KindEditor.create('#'+div_id, {
                    uploadJson : Kind_upload_json,
                    fileManagerJson : Kind_file_manager_json,
                    resizeType : 1,
                    allowPreviewEmoticons : true,
                    allowImageUpload : true,
                    width : '100%',
                    height: '400px',
                    afterChange: function(){
                        this.sync();
                    },
                    filterMode:false,
                    afterBlur : function(){
                        this.sync();
                    },
                    items : [
                        'source','fontname', '|','fontsize', 'forecolor', 'lineheight','table','hilitecolor', 'bold', 'italic', 'underline',
                        'removeformat', '|', 'justifyleft', 'justifycenter', 'justifyright', 'insertorderedlist',
                        'insertunorderedlist','link','|','image','multiimage','insertfile','|','fullscreen','subscript','superscript','quickformat','pagebreak']
                });
        }
        $(".wy-edit").each(function(){
            var div_id = $(this).attr("id");
            layedit_init(div_id);
        });

        exports('global', {})
    });
}